package com.li.springboard.controller;

import com.li.springboard.mapper.DepartmentDao;
import com.li.springboard.mapper.EmployeeDao;
import com.li.springboard.pojo.Department;
import com.li.springboard.pojo.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Collection;


@Controller
public class EmployeeController {
    @Resource
    EmployeeDao employeeDao;
    @Resource
    DepartmentDao departmentDao;

    @RequestMapping("/main")
    public String Main(){
        return "redirect:/main.html";
    }
    @RequestMapping("/emps")
    public String query(Model model){
        Collection<Employee> employees=employeeDao.getAll();
        model.addAttribute("employ",employees);
        return "emp/list";
    }

    @GetMapping("/emp")
    public String add(Model model){
        Collection<Department> departments=departmentDao.getDepartments();
        model.addAttribute("dept",departments);
        return "emp/add";
    }
    @PostMapping ("/emp")
    public String addEmp(Employee employee){
        System.out.println("debug-->"+employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @GetMapping("/emp/{id}")
    public String update(@PathVariable("id") Integer id,Model model){
        //查询员工表
        Employee employees=employeeDao.getEmployeeById(id);
        model.addAttribute("eml",employees);
        //查询部门表
        Collection<Department> departments=departmentDao.getDepartments();
        model.addAttribute("dept",departments);
        return "emp/update";
    }

    @PostMapping ("/update")
    public String updateEmp(Employee employee){
        System.out.println("debug-->"+employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        //查询员工表
        employeeDao.delete(id);
        return "redirect:/emps";
    }
}
